package com.jxc.wiki.controller;

import com.jxc.wiki.request.CategoryQueryParamReq;
import com.jxc.wiki.request.CategorySaveParamReq;
import com.jxc.wiki.response.CommonResp;
import com.jxc.wiki.response.CategoryQueryParamResp;
import com.jxc.wiki.response.PageParamResp;
import com.jxc.wiki.service.CategoryService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@Controller
public class CategoryController {

    @Resource
    private CategoryService categoryService;

    // 测试get请求
    @RequestMapping("/category/all")
    @ResponseBody
    // @Valid 用来开启校验，防止有人通过脚本修改后端获取的数据数，从而导致服务器崩溃
    public CommonResp<List<CategoryQueryParamResp>> all(){
        CommonResp<List<CategoryQueryParamResp>> resp = new CommonResp<>();
        List<CategoryQueryParamResp> list = categoryService.all();
        resp.setContent(list);
        return resp;
    }

    @PostMapping("/category/save")
    @ResponseBody
    // 注意由于axios的post请求提交是用json的格式，所以在参数前要加个@RequestBody，相当于标记数据为application/json格式
    public CommonResp save(@Valid @RequestBody CategorySaveParamReq req){
        CommonResp resp = new CommonResp<>();
        categoryService.save(req);
        return resp;
    }


    @DeleteMapping("/category/delete/{id}")
    @ResponseBody
    public CommonResp delete(@PathVariable Long id){
        CommonResp resp = new CommonResp<>();
        categoryService.delete(id);
        return resp;
    }

    // 测试get请求
    @RequestMapping("/category/search")
    @ResponseBody
    // @Valid 用来开启校验，防止有人通过脚本修改后端获取的数据数，从而导致服务器崩溃
    public CommonResp<PageParamResp<CategoryQueryParamResp>> search(@Valid CategoryQueryParamReq req){
        CommonResp<PageParamResp<CategoryQueryParamResp>> resp = new CommonResp<>();
        PageParamResp<CategoryQueryParamResp> list = categoryService.list(req);
        resp.setContent(list);
        return resp;
    }
}
